package com.zhongjh.imageingstudy.view.image.help;

import android.content.Context;
import android.util.Log;
import android.view.GestureDetector;
import android.view.MotionEvent;

import com.zhongjh.imageingstudy.view.image.ActionImageView;


public class MoveAdapter extends GestureDetector.SimpleOnGestureListener {

    private final String TAG = MoveAdapter.class.getSimpleName();

    ActionImageView mActionImageView;

    public MoveAdapter(ActionImageView actionImageView) {
        mActionImageView = actionImageView;
    }

    @Override
    public boolean onDown(MotionEvent e) {
        return true;
    }

    @Override
    public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
        return onScroll(distanceX, distanceY);
    }

    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
        return super.onFling(e1, e2, velocityX, velocityY);
    }

    /**
     * 滑动事件
     *
     * @param dx x轴
     * @param dy y轴
     * @return false:继续下一个触屏
     */
    private boolean onScroll(float dx, float dy) {
        Log.d(TAG, "onScroll " + mActionImageView.getScaleX() + " " + mActionImageView.getScrollY());
        mActionImageView.mActionImage.onScroll(mActionImageView.getScrollX(), mActionImageView.getScrollY(), -dx, -dy);
        // 每次移动重新绘画
        mActionImageView.invalidate();
        return false;
    }

    /**
     * 设置图片回归原位
     */
    private boolean scrollTo(int x, int y) {
        if (mActionImageView.getScrollX() != x || mActionImageView.getScrollY() != y) {
            mActionImageView.scrollTo(x, y);
            return true;
        }
        return false;
    }
}
